package com.example.zhish.animator_property_pop_menu_demo001;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationSet;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    int[] imagesViewID = {R.id.image_a, R.id.image_b, R.id.image_c, R.id.image_d,
            R.id.image_e, R.id.image_f, R.id.image_g, R.id.image_h};
    List<ImageView> list = new ArrayList<ImageView>();
    boolean flag = true;
    int radius = 240;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        for (int i = 0; i < imagesViewID.length; i++) {

            ImageView imageView = (ImageView) findViewById(imagesViewID[i]);
            list.add(imageView);
            imageView.setOnClickListener(this);

        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_a:
                if (flag) {
                    flag = false;
                    //startAnim();
                    startSectorMenu();
                } else {
                    flag = true;
                    //closeAnim();
                    closeSectorMenu();
                }

                break;
            case R.id.image_b:
                break;
            case R.id.image_c:
                break;
            case R.id.image_d:
                break;
            case R.id.image_e:
                break;
            case R.id.image_f:
                break;
            case R.id.image_g:
                break;
            case R.id.image_h:
                break;

        }
    }

    private void closeAnim() {

        for (int i = 1; i < imagesViewID.length; i++) {
            ObjectAnimator animator = new ObjectAnimator().ofFloat(list.get(i), "translationY", i * 60, 0f);
            animator.setDuration(150);
            animator.setStartDelay(i * 100);
            animator.start();
        }
    }

    private void startAnim() {
        for (int i = 1; i < imagesViewID.length; i++) {
            Log.i("mike", list.size() + "--list_size");
            Log.i("mike", list.get(7).getId() + "");
            ObjectAnimator animator = new ObjectAnimator().ofFloat(list.get(i), "translationY", 0f, i * 60);
            animator.setDuration(150);
            animator.setStartDelay(i * 100);
            animator.start();
        }
    }

    private void startSectorMenu() {
        for (int i = 1; i < imagesViewID.length; i++) {
            ObjectAnimator animator = new ObjectAnimator().ofFloat(list.get(i), "translationY", 0f, getY(i));
            ObjectAnimator animator1 = new ObjectAnimator().ofFloat(list.get(i), "translationX", 0f, getX(i));
            Log.i("mike", "--X:" + getX(i));
            Log.i("mike", "--Y:" + getY(i));
            animator.setStartDelay(i * 100);
            AnimatorSet set = new AnimatorSet();
            set.play(animator).with(animator1);
            set.setDuration(150);
            set.start();
        }
    }

    private void closeSectorMenu() {
        for (int i = 1; i < imagesViewID.length; i++) {
            ObjectAnimator animator = new ObjectAnimator().ofFloat(list.get(i), "translationY", getY(i), 0f);
            ObjectAnimator animator1 = new ObjectAnimator().ofFloat(list.get(i), "translationX", getX(i), 0f);

            animator.setStartDelay(i * 100);
            AnimatorSet set = new AnimatorSet();
            set.play(animator).with(animator1);
            set.setDuration(150);
            set.start();
        }
    }

    private int getX(int xNum) {

        double angle = (xNum - 1) * 90.0 * Math.PI / 180 / 6.0;
        int x = (int) (Math.cos(angle) * radius);
        return x;
    }

    private int getY(int yNum) {

        double angle = (yNum - 1) * 90.0 * Math.PI / 180 / 6.0;
        Log.i("mike","yyyy:"+angle+"~~~:"+yNum);
        int y = (int) (Math.sin(angle) * radius);
        return y;

    }
}
